Moduł:ImageLink
Autor: Nanaki -- Źródło: http://pl.leagueoflegends.wikia.com/wiki/Modu%C5%82:ImageLink local ImageLink = {} -- basic | link = | text = | image = | size = | width = | height = | border = | separator = | display = | label = | class = | style = | iconclass = | iconstyle = | labelclass = | labelstyle = }} ---- function ImageLink.basic(frame) local args = arguments(frame.args) local link, img, label = ImageLink._createLink(args) return link end function ImageLink._createLink(args) args'display' = (({'inline-block' = 'inline-block','inlineblock' = 'inline-block','block' = 'block','inline' = 'inline'})[args'display' or 'inline']) or 'inline' args'label' = args'label' or 'after' args'image' = args'image' or '' args'border' = ternary(args'border', true, false) local width, height if(args'size') then width, height = getSize(args'size') end args'height' = tonumber(args'height') or height or nil args'width' = tonumber(args'width') or width or nil args'selflink' = args'selflink' or mw.title.getCurrentTitle().prefixedText args'link' = args'link' or '' if args'link' '*none*' then args'link' = '' end args'text' = args'text' or args'link' or '' args'separator' = args'separator' or '' if args'text' '*none*' then args'text' = '' end if args'text' '' then args'label' = 'none' end local tag = ternary(args'display' 'inline', 'span', 'div') local link = mw.html.create(tag):css('display', args'display'):addClass(args'display' .. '-image') if args'class' then link:addClass(args'class') end if args'style' then link:cssText(args'style') end local label = mw.html.create(tag) if args'label' 'before' or args'label' 'after' then link:addClass('label-'..args'label') if args'labelclass' then label:addClass(args'labelclass') end if args'labelstyle' then label:cssText(args'labelstyle') end if args'link' args'selflink' then label:wikitext('' .. args'text' .. ''):done() elseif args'link' '' then label:wikitext(args'text'):done() else label:wikitext(formatnil('%s', args'link', args'text')):done() end else label = nil end local img = mw.html.create(tag) if args'image' ~= '' then local size = ternary(args'height', tostring(args'width' or '')..'x'..tostring(args'height' or '')..'px', tostring(args'width' or '')..'px') if args'iconclass' then img:addClass(args'iconclass') end if args'width' then img:attr('data-width', tostring(args'width')) end if args'height' then img:attr('data-height', tostring(args'height')) end if args'border' then img:addClass('border') end if args'iconstyle' then img:cssText(args'iconstyle') end img:wikitext(mw.ustring.format(' ', args'image', size, ternary(args'border', 'border|', ''), ternary(args'link' args'selflink', '', args'link'), args'text')):done() else img = nil end if args'label' 'before' then if label then link:node(label) end if args'separator' and img and label then link:wikitext(args'separator') end if img then link:node(img) end elseif args'label' 'after' then if img then link:node(img) end if args'separator' and img and label then link:wikitext(args'separator') end if label then link:node(label) end else if img then link:node(img) end end return link, img, label end function formatnil(text, ...) if select("#", ...) 0 then return text end for i=1, select("#", ...) do if select(i, ...) nil then return nil end end return mw.ustring.format(text, ...) end function append(orig, new) if new then if mw.ustring.sub(new, 1, 1) '-' then if mw.ustring.sub(new, 2, 2) '-' then return mw.ustring.sub(new, 2) else return orig .. mw.ustring.sub(new, 2) end else return new end else return nil end end function getSize(size) local fields = mw.text.split(mw.ustring.gsub(tostring(size or ''), 'px', '', 1), 'x') local width = tonumber(fields1) or nil local height = tonumber(fields2) or nil return width, height end function deHash(text, sel) if text nil then return nil, nil end local res, hash, offset = nil, nil, nil text = tostring(text) offset = mw.ustring.find(text, '#', 1, true) if offset then res = mw.ustring.sub(text, 1, offset-1) hash = mw.ustring.sub(text, offset+1) else res = text hash = nil end if sel nil then return res, hash else return select(sel, res, hash) end end function ternary(cond, T, F) if cond then return T else return F end end function arguments(origArgs) local args = {} for k, v in pairs(origArgs) do if type(v) 'string' then v = mw.text.trim(v) end if v ~= '' then argsk = v end end return args end return ImageLink --[[ return ImageLink._createIcon{ display = args'display', label = argsimage = args['image', border = args'border', width = args'width', height = args'height', link = args'link', text = args'text', separator = args'separator', class = args'class', style = args'style', iconclass = args'iconclass', iconstyle = args'iconstyle', labelclass = args'iconclass', labelstyle = args'labelstyle', } --]]